/**
 * related to edit.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-08-04
 */
var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");
var nf = sm("do_Notification");
var root = ui("$");
var openHelper = require("helper/open");
var imgTool = require("helper/picture");
var qqHttp = require("helper/http");
var urlUser = require("url/user");

var linearLayout_main = ui("do_LinearLayout_main");
var do_Button_1 = ui("do_Button_1");
var picLayout01 = ui("picLayout01");
var picLayout02 = ui("picLayout02");
var picLayout03 = ui("picLayout03");
var auditStatus = ui("auditStatus");
var pic01 = ui("pic01");
var pic02 = ui("pic02");
var pic03 = ui("pic03");
var imgLayout01Close = ui("imgLayout01Close");
var imgLayout02Close = ui("imgLayout02Close");
var imgLayout03Close = ui("imgLayout03Close");
var jsonData = {};
var fromPage = null;
//andriod返回键
page.on("back", function() {
    app.closePage();
});
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage();
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "身份信息"
})
header.refreshData();
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var data = page.getData();
	if (data != null) {
		if (data.fromPage != null && data.fromPage != '') {
			fromPage = data.fromPage;
		}
	}
	page.hideKeyboard();
	getData();
});
var realNameText = ui("realNameText"); //姓名
var idNumberText = ui("idNumberText"); //身份证号

function getData() {
	qqHttp.post(urlUser.url.auth.detail, {}, function(data) {
		deviceone.print(JSON.stringify(data));
		var userAuthVo = data.userAuthVo;
		if (userAuthVo != null) {
			realNameText.text = userAuthVo.realName; //姓名
			idNumberText.text = userAuthVo.identityNumber; //身份证
			//身份证正面照
			if (userAuthVo.identityCardImg != null && userAuthVo.identityCardImg != '') {
				pic01.source=userAuthVo.fileDomain + userAuthVo.identityCardImg;
				jsonData.identityCardImg = userAuthVo.identityCardImg;
			}
			//身份证反面照
			if (userAuthVo.identityCardBackImg != null && userAuthVo.identityCardBackImg != '') {
				pic02.source=userAuthVo.fileDomain + userAuthVo.identityCardBackImg;
				jsonData.identityCardBackImg = userAuthVo.identityCardBackImg;
			}
			//个人正面照
			if (userAuthVo.photosImg != null && userAuthVo.photosImg != '') {
				pic03.source=userAuthVo.fileDomain + userAuthVo.photosImg;
				jsonData.photosImg = userAuthVo.photosImg;
			}
			var aStatus = userAuthVo.auditStatus;
			if (aStatus == 2) {
				auditStatus.text = "已通过";
				do_Button_1.visible = false;
			} else if (aStatus == 1) {
				auditStatus.text = "审核中";
				imgLayout01Close.visible = true;
				imgLayout02Close.visible = true;
				imgLayout03Close.visible = true;
			} else if (aStatus == 3) {
				auditStatus.text = "不通过，请重新提交";
				imgLayout01Close.visible = true;
				imgLayout02Close.visible = true;
				imgLayout03Close.visible = true;
			}
		}
	});
}

/*正面*/
picLayout01.on("touch", function(data, e) {
	imgTool.imgMulti(1, selectImgBack1);// 选择多张图片
});
function selectImgBack1(imgList) {
	if(imgList != null){
		pic01.source=imgList[0];
		qqHttp.upload(imgList[0],"user/auth",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			jsonData.identityCardImg = string4imgs;
		});
	} else{
		jsonData.identityCardImg = "";
	}
}

/*反面*/
picLayout02.on("touch", function(data, e) {
	imgTool.imgMulti(1, selectImgBack2);// 选择多张图片
});

function selectImgBack2(imgList) {
	if(imgList != null){
		pic02.source=imgList[0];
		qqHttp.upload(imgList[0],"user/auth",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			jsonData.identityCardBackImg = string4imgs;
		});
	} else{
		jsonData.identityCardBackImg = '';
	}
}
/*手持*/
picLayout03.on("touch", function(data, e) {
	imgTool.imgMulti(1, selectImgBack3);// 选择多张图片
});
function selectImgBack3(imgList) {
	if(imgList != null){
		pic03.source=imgList[0];
		qqHttp.upload(imgList[0],"user/auth",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			jsonData.photosImg = string4imgs;
		});
	} else{
		jsonData.photosImg = '';
	}
}

//确定
do_Button_1.on("touch","",300, function(data, e) {
	submit();
});

function submit(){
	page.hideKeyboard();
	jsonData.realName = realNameText.text.trim();
	jsonData.identityNumber = idNumberText.text.trim();
	var msg = [];
	if(jsonData.realName == null || jsonData.realName==""){
		msg.push("请输入真实姓名");
	}
	if(jsonData.identityNumber == null || jsonData.identityNumber==""){
		msg.push("请输入身份证号");
	}
	if (jsonData.identityCardImg == null || jsonData.identityCardImg == "") {
		msg.push("请添加身份证正面照");
	}
	if (jsonData.identityCardBackImg == null || jsonData.identityCardBackImg == "") {
		msg.push("请添加身份证反面照");
	}
	if (jsonData.photosImg == null || jsonData.photosImg == "") {
		msg.push("请添加个人正面照");
	}
	if (msg.length > 0) {
		nf.toast(msg.toString());
		return;
	}
	nf.confirm({text:"是否确认提交?",title:"提示",button1text:"提交",button2text:"取消"},function(datacall,e){
		if(datacall == 1){
			qqHttp.post(urlUser.url.auth.edit, jsonData, function(data) {
				if (fromPage != null && fromPage == 'addShop') {
					nf.toast("提交成功,开始创建店铺吧");
					openHelper.open("source://view/qshop/manage/shop/addShop2.ui");
				} else {
					nf.toast("提交成功,等待审核");
					app.closePage();
				}
			});
		}
	});
}
ui("do_ScrollView_1").redraw();
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})
imgLayout01Close.on("touch",function(){
	pic01.source='';
	jsonData.identityCardImg = '';
	imgLayout01Close.visible = false;
});
imgLayout02Close.on("touch",function(){
	pic02.source='';
	jsonData.identityCardBackImg = '';
	imgLayout02Close.visible = false;
});
imgLayout03Close.on("touch",function(){
	pic03.source='';
	jsonData.photosImg = '';
	imgLayout03Close.visible = false;
});